package com.mybatis.dome;

import com.mybatis.io.Resources;
import com.mybatis.sqlSession.SqlSession;
import com.mybatis.sqlSession.SqlSessionFactory;
import com.mybatis.sqlSession.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;


public class IUserDaoImpl implements IUserDao{
    @Override
    public List<User> selectList() throws PropertyVetoException, DocumentException {
        SqlSession sqlSession = getSqlSession();
        IUserDao dao = sqlSession.getMapper(IUserDao.class);
        List<User> users = dao.selectList();
        return users;
    }

    @Override
    public User selectOne(User user) throws PropertyVetoException, DocumentException {
        SqlSession sqlSession = getSqlSession();
        IUserDao dao = sqlSession.getMapper(IUserDao.class);
        User user1 = dao.selectOne(user);
        return user1;
    }

    public SqlSession getSqlSession() throws PropertyVetoException, DocumentException {
        InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = build.openSession();
        return sqlSession;
    }
}
